package com.zjw.zy.base;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/27 13:26
 */
public abstract class BaseRecyclerAdapter<DATA, VH extends BaseViewHolder<DATA>> extends RecyclerView.Adapter<VH> {

    protected static final int TYPE_HEAD = 11;
    protected static final int TYPE_TAIL = 12;

    private final List<DATA> mData = new ArrayList<>();

    protected final ItemListener<VH> mItemListener;

    public BaseRecyclerAdapter() {
        mItemListener = null;
    }

    public BaseRecyclerAdapter(ItemListener<VH> listener) {
        mItemListener = listener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void notifyData(List<DATA> data) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    /**
     * 不同性比较
     */
    public void notifyDataWithDiff(List<DATA> data, BaseDiffCallback<DATA> callback) {
        callback.setData(mData, data);
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(callback, true);

        diffResult.dispatchUpdatesTo(this);

        mData.clear();
        mData.addAll(data);
    }

    /**
     * 不同性比较
     */
    public void notifyDataWithDiff(List<DATA> data) {
        notifyDataWithDiff(data, new BaseDiffCallback<>());
    }


    public void notifyItemData(DATA data, int position) {
        mData.set(position, data);
        notifyItemChanged(position);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        if (position < getHeadCount()) {
            onBindHeader(position);
            return;
        }
        int realPosition = position - getHeadCount();
        if (realPosition >= mData.size()) {
            onBindTail(realPosition - mData.size());
            return;
        }
        DATA data = mData.get(realPosition);
        holder.bindData(data, position);
        holder.bindListener((ItemListener<BaseViewHolder<DATA>>) mItemListener);
    }

    protected void onBindTail(int position) {

    }

    protected void onBindHeader(int position) {

    }

    @NonNull
    public List<DATA> getData() {
        return mData;
    }

    @Override
    public int getItemCount() {
        return mData.size() + getHeadCount() + getTailCount();
    }

    @Override
    public int getItemViewType(int position) {
        if (position < getHeadCount()) {
            return TYPE_HEAD;
        }
        if (position >= getHeadCount() + getData().size()) {
            return TYPE_TAIL;
        }
        return super.getItemViewType(position);
    }

    protected int getHeadCount() {
        return 0;
    }

    protected int getTailCount() {
        return 0;
    }

    public interface ItemListener<Holder extends BaseViewHolder<?>> {

        default void onItemClick(@NonNull Holder holder, @NonNull View v) {

        }

        default boolean onItemLongClick(@NonNull Holder holder, @NonNull View v) {
            return false;
        }

        default void onItemDoubleClick(@NonNull Holder holder, @NonNull View v) {

        }

        default void onCallbackOtherEvent(@NonNull String event, Object... args) {
        }
    }
}
